import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex);

let state = {
    buys : [],
    collects : [],
    historys : [],
    total: 0,
    totals:0
}

let mutations = {
    toCar(state,p){
        //state.buys.push(p)
        //state.buys[p.productPromo.advWords] = p;
        state.buys.push(p)
        console.log(state)
    },
    tocollect(state,p){
    	if(state.collects.p){
    		
    	}else{
    		state.collects.push(p)
    	}
    	
    },
    tohistory(state,l){
    	state.historys.unshift(l)
    },
    remove(state,index) {
        console.log(index)
        state.buys.splice(index,1)
    },
    reduce(state,index){
    	state.buys[index].num--;
        if (state.buys[index].num < 1) {
            state.buys.splice(index,1)
        }	
    },
    add(state,index){
    	state.buys[index].num++;
    },
    zong(state){
    	state.total = 0;
    	state.totals = 0;
    	for(var i=0; i<state.buys.length;i++) {
			state.total = state.total + (state.buys[i].num*state.buys[i].productPromo.price)
			state.totals = state.totals + state.buys[i].num
		}
    }
}
let getters = {
   
}
export default new Vuex.Store({
    state,
    mutations,
    getters
})
